package club.justwill.single.persistence.dao;

import club.justwill.single.model.AuditType;
import club.justwill.single.model.ProjectType;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * Created by xiangnan on 15-12-26.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ProjectDao extends BaseDao<Projects> {

    public List<Projects> searchProject(int index, int size, int status) {
        Query query = em.createQuery("select pj from Projects pj where pj.status = :status order by pj.projectId")
                .setParameter("status",status)
                .setFirstResult(index).setMaxResults(size);

        return (List<Projects>) query.getResultList();
    }

    public Projects getByProjectId(long projectId) {
        Query query = em.createNamedQuery("Project.getByProjectId").setParameter("projectId", projectId);
        try {
            return (Projects) query.getSingleResult();
        } catch (NonUniqueResultException e) {
            return (Projects) query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }


    public List<Projects> searchProjectsByModelId(long modelId, int projectIndex, int projectSize) {
        Query query = em.createNamedQuery("Project.searchProjectsByModelId").setParameter("modelId",modelId)
                .setFirstResult(projectIndex)
                .setMaxResults(projectSize);
        return query.getResultList();
    }

    public Projects getByUserId(long userId) {
        String hql = "select p from Projects p, Skus s, OrderItem oi, Orders o where p.projectId = s.projectId and s.skusId = oi.skuId and oi.orderId = o.orderId and o.userId = :userId group by o.userId";
        Query query = em.createQuery(hql);
        query.setParameter("userId",userId);
        try {
            return (Projects) query.getSingleResult();
        } catch (NonUniqueResultException e) {
            return (Projects) query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }

    public Projects getByOrderId(long orderId) {
        Query query = em.createQuery("select p from Projects p, Skus s,OrderItem oi where p.projectId = s.projectId and s.skusId = oi.skuId and oi.orderId = :orderId");
        query.setParameter("orderId", orderId);
        return (Projects) query.getSingleResult();
    }

    public List<Projects> searchProjectsByIdList(int index, int size, List<Long> projectIdList) {

        Query query = em.createNamedQuery("Project.searchProjectsByIdList")
                .setParameter("followedProjectIdList",projectIdList)
                .setFirstResult(index)
                .setMaxResults(size);
        return query.getResultList();
    }

    public List<Projects> getProjectByTag(int tag, int status, int index, int size) {
        Query query = em.createQuery("select pj from Projects pj where pj.status = :status and pj.tags = :tag  order by  pj.processTimeStamp")
                .setParameter("tag",tag)
                .setParameter("status",status)
                .setFirstResult(index)
                .setMaxResults(size);
        return query.getResultList();
    }

    public List<Projects> getProjects(int index, int size) {
        Query query = em.createNamedQuery("Project.search").setFirstResult(index)
                .setMaxResults(size);
        return query.getResultList();
    }

    public List<Projects> getProjectByStatus(int status, int index, int size) {
        Query query = em.createQuery("select pj from Projects pj where pj.status = :status order by  pj.processTimeStamp")
                .setParameter("status", status)
                .setFirstResult(index)
                .setMaxResults(size);
        return query.getResultList();
    }

    public List<Projects> getFinishedProject() {
        Query query = em.createNamedQuery("Project.getFinishedProject");
        return query.getResultList();
    }

}
